package com.cyt.sys.service.impl;

import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.sys.dao.SysGuideDao;
import com.cyt.sys.model.po.SysGuide;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.model.vo.SysGuideVo;
import com.cyt.sys.service.SysGuideService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 操作指引-操作指引Service层实现类
 *
 * @author rong yuan
 * @date 2019-8-20 13:52:09
 */
@Service("sysGuideService")
public class SysGuideServiceImpl extends BaseServiceImpl<SysGuide> implements SysGuideService {

    @Resource
    private SysGuideDao sysGuideDao;

    @Autowired
    public void setBaseDao(SysGuideDao sysGuideDao) {
        this.baseDao = sysGuideDao;
    }

    /**
     * 单个对象查询
     *
     * @param sysGuideVo 接受参数对象
     * @return SysGuide
     * @author rong yuan
     * @date 2019-8-20 13:52:09
     */
    @Override
    public SysGuide findOne(SysGuideVo sysGuideVo) {
        SysGuide sysGuide = new SysGuide();
        if (StringUtils.isBlank(sysGuideVo.getId())) {
            sysGuide.setId(sysGuideVo.getId());
        }
        if (StringUtils.isBlank(sysGuideVo.getLoginName())) {
            sysGuide.setId(sysGuideVo.getLoginName());
        }
        return sysGuideDao.selectOne(sysGuide);
    }

    /**
     * 添加或更新
     *
     * @param sysGuideVo 参数接受对象
     * @param sysUser    当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-8-20 13:52:09
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(SysGuideVo sysGuideVo, SysUser sysUser) {
        if (StringUtils.isBlank(sysGuideVo.getId())) {
            SysGuide sysGuide = new SysGuide(sysUser);
            sysGuide.setLoginName(sysUser.getLoginName());
            sysGuide.setStatus(sysGuideVo.getStatus());
            sysGuideDao.insert(sysGuide);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            SysGuide sysGuide = sysGuideDao.selectByPrimaryKey(sysGuideVo.getId());
            sysGuide.setLoginName(sysUser.getId());
            sysGuide.setStatus(sysGuideVo.getStatus());
            sysGuide.setUpdateTime(new Date());
            sysGuide.setUpdateUser(sysUser.getId());
            sysGuideDao.updateByPrimaryKey(sysGuide);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }
}